package cn.kgc.controller;

import cn.kgc.bean.pojo.CRole;
import cn.kgc.common.Constants;
import cn.kgc.common.Result;
import cn.kgc.service.RoleManagerService;
import com.alibaba.fastjson.JSONObject;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

@RestController
public class RoleManagerController {
    @Resource
    private RoleManagerService rms;

    //查
    @RequestMapping("/selectrole")
    public Result selectrole(@RequestParam Integer pageNum,@RequestParam Integer pageSize){
        PageHelper.startPage(pageNum,pageSize);
        List<CRole> list = rms.selectAll();
        PageInfo<CRole> info = new PageInfo<>(list);
        return Result.success(Constants.CODE_200,"",info);
    }

    //删
    @RequestMapping(value = "/deleterole/{id}" ,method = RequestMethod.POST)
    public Result deleteRole(@PathVariable long id){
        boolean flag = rms.deleteRoleById(id);
        if (flag == true){
            return Result.success(flag);
        }else{
            return Result.error();
        }
    }

    //增
    @RequestMapping(value = "/addrole" ,method = RequestMethod.POST)
    public Result addRole(@RequestBody CRole cRole){
        boolean flag = rms.addRole(cRole);
        if (flag == true){
            return Result.success(flag);
        }else{
            return Result.error();
        }
    }

    //改
    @RequestMapping(value = "/updaterole",method = RequestMethod.POST)
    public Result updateRole(@RequestBody CRole cRole){
        boolean flag = rms.updateRole(cRole);
        if (flag == true){
            return Result.success(flag);
        }else{
            return Result.error();
        }
    }

    //模糊查询
    @RequestMapping("/selectByName")
    public Result selectByName(@RequestParam Integer pageNum,@RequestParam Integer pageSize,@RequestParam String name){
        PageHelper.startPage(pageNum,pageSize);
        List<CRole> list = rms.selectByName(name);
        PageInfo<CRole> info = new PageInfo<>(list);
        return Result.success(Constants.CODE_200,"",info);
    }
}
